package com.bbzn.device.client.controller;

import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.AuditConstant;
import com.bbzn.device.client.constant.SystemParamConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.utils.*;
import com.bbzn.user.client.TenantClient;
import com.bbzn.user.client.UserClient;
import com.bbzn.user.comment.User;
import com.bbzn.user.comment.res.TenantRes;
import com.bbzn.user.comment.res.UserRes;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 蒋洪媛
 * @Date 2021/10/28 15:59
 * @Version 1.0
 */

@RestController
@RequestMapping("/comment")
@Slf4j
@Api(value = "comment", tags = "公共Controller ")
public class CommentController extends BaseApiService {

    @Autowired
    private CommonService commentService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private UserClient userClient;

    @Autowired
    private TenantClient tenantClient;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 同级显示 获取所有子用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getUserIds", method = RequestMethod.POST)
    @OperationAnnotation(content = "获取所有子用户信息")
    public BaseResponse getUserIds(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commentService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commentService.getUserId(request);

        List<Long> ids = commentService.getUserIds(userId);
        if(CollectionUtils.isEmpty(ids)){
            systemParam = systemParamByCacheService.getValues(SystemParamConstant.SYSTEM_PARAM_ENUM, SystemParamConstant.SYSTEMPARAM_NO_DATA_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        List<User> users = userClient.selectBatchIds(ids);
        return setResultSuccess(users);
    }

    /**
     * 递归树状 获取所有子用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getChildByUser", method = RequestMethod.POST)
    @OperationAnnotation(content = "递归树状：获取所有子用户信息")
    public BaseResponse getChildByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commentService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commentService.getUserId(request);

        User user = new User();
        user.setId(userId);
        List<UserRes> userRes = commentService.getChildByUser(user);
        if(CollectionUtils.isEmpty(userRes)){
            systemParam = systemParamByCacheService.getValues(SystemParamConstant.SYSTEM_PARAM_ENUM, SystemParamConstant.SYSTEMPARAM_NO_DATA_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        return setResultSuccess(userRes);
    }

    /**
     * 查询当前用户所属公司-获取该公司下所有用户信
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTenantByUser", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询当前用户所属公司-获取该公司下所有用户信息")
    public BaseResponse getTenantByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commentService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commentService.getUserId(request);

        List<User> userList = commentService.getTenantByUser(userId, language);
        return setResultSuccess(userList);
    }
}
